package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.ChapterActionImpl;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.DepartMentActionImpl;
import com.hrbxlh.actionImpl.HomeworkTestActionImpl;
import com.hrbxlh.actionImpl.MyCourseActionImpl;
import com.hrbxlh.actionImpl.RelationActionImpl;
import com.hrbxlh.util.ExcelsUtil;
import com.hrbxlh.util.FileUpload;
import com.hrbxlh.util.Result;
import com.hrbxlh.util.ResultEnum;
import com.hrbxlh.util.SessionValues;
import com.hrbxlh.vo.SkillDTO;
import com.hrbxlh.vo.SkillNotes;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：CourseAction.java
* Description：课程action类
* History：
* Date           Author               Desc
* 2017.3.27		  xzx                 建立
* -------------------------------------------------------
*/

@Controller
public class CourseAction {
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	@Autowired
	@Qualifier("chapterActionImpl")
	private ChapterActionImpl chapterActionImpl;
	
	@Autowired
	@Qualifier("myCourseActionImpl")
	private MyCourseActionImpl myCourseActionImpl;
	
	@Autowired
	@Qualifier("homeworkTestActionImpl")
	private HomeworkTestActionImpl homeworkTestActionImpl;
	
	@Autowired
	@Qualifier("relationActionImpl")
	private RelationActionImpl relationActionImpl;
	
	@Autowired
	@Qualifier("departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;
	
	/**
	 * @description 进页面要把登录人的名字带进来，赋值到教师栏，
					带该教师所属的学院的全部专业列表，然后选择专业方向（下拉）
	 * @param request
	 * @param response
	 * @return  跳转到添加课程页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipAddCourse", method = RequestMethod.GET)
	public ModelAndView skipAddCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"userName","stuorteaId"}, request);
			resultMap = sessionValues;
			//通过教师工号获取全部专业
			List<Map<String, Object>> professionList = courseActionImpl.getMyProfessionList(resultMap);
			resultMap.put("professionList", professionList);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加课程页面名称
			pageName = "pc/addCourse";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转添加课程页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 添加课程
	 * @param request 
	 * @param response
	 * @return  课程主键id courseId
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addCourse", method = RequestMethod.POST)
	public void  addCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值 
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			//添加课程
			Map<String, Object> addResultMap = courseActionImpl.addCourse(paramMap);
			boolean flag = (Boolean)addResultMap.get("resultFlag");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", addResultMap.get("failMsg"));
			}
			//返回添加的课程信息
			resultMap.put("courseMsg", addResultMap.get("detailMsg"));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加课程出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 查询全部院系post的
	 * @param request 
	 * @param response
	 * @return  
	 * @throws IOException
	 */
	@RequestMapping(value = "/getDepartmentListPost", method = RequestMethod.POST)
	public void  getDepartmentListPost (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try { 
			//全部院系
			List<Map<String, Object>> departmentList = departMentActionImpl.getDepartmentAll(new HashMap<String, Object>());
			resultMap.put("departmentList", departmentList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加课程出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**跳转到修改课程页面
	 * @description 进页面要把登录人的名字带进来，赋值到教师栏，
		带该教师所属的学院的全部专业列表，然后选择专业方向（下拉） 
	 * @param request
	 * @param response
	 * @return  跳转到修改课程页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveCourse", method = RequestMethod.GET)
	public ModelAndView skipSaveCourse (String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取页面参数值
			Map<String,Object> preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取课程信息
			Map<String, Object> courseDetail = courseActionImpl.getCourseDetail(preParamMap,request);
			//获取章节信息
			List<Map<String, Object>> chapterList = chapterActionImpl.getCourseChapter(preParamMap);
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"userName","stuorteaId"}, request);
			resultMap = sessionValues;
			//通过教师工号获取全部专业
			List<Map<String, Object>> professionList = courseActionImpl.getMyProfessionList(resultMap);
			resultMap.put("professionList", professionList);
			resultMap.put("course", courseDetail);
			resultMap.put("chapterList", chapterList);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//修改课程页面名称
			pageName = "pc/saveCourse";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace(); 
			logger.error("跳转修改课程页面出现异常", e);
			pageName = "public/pageerror"; 
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 修改课程
	 * @param request
	 * @param response
	 * @return  
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveCourse", method = RequestMethod.POST)
	public void  saveCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			Map<String, Object> saveResultMap = courseActionImpl.saveCourse(paramMap);
			boolean flag = (Boolean)saveResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(!flag){
				resultMap.put("failMsg", saveResultMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改课程出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 删除课程
	 * @param request   List<String>形式   jsonStr = ["1","2","3"...]
	 * @param response
	 * @return  
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delCourse", method = RequestMethod.POST)
	public void  delCourse (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try { 		
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
			boolean flag = courseActionImpl.delCourse(paramList);
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除课程出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 查询某章节名称是否存在
	 * @param request  chapterName:
	 * @param response
	 * @return  
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getChapterExistMark", method = RequestMethod.POST)
	public void  getChapterExistMark (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try { 
			//获取参数值
			Map<String, Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取章节名称是否存在
			List<Map<String, Object>> list = chapterActionImpl.getChapterByName(paramMap);
			if(list != null && list.size() != 0){
				resultMap.put("result", "exists");
			}else{
				resultMap.put("result", "notexists");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取章节出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/***
	 * @description 添加章节
	 * @param response
	 * @param request
	 * @param chapterId
	 * @param chapterNum
	 * @param chapterName
	 * @param type
	 * @param testAddress
	 * @param courseware
	 * @param video
	 * @param homework
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/addChapterMessage", method = RequestMethod.POST)
	public String  addChapterMessage  (HttpServletRequest request,
			long courseId,
			String chapterName,
			long chapterNum,
			long type,
			long addTestMark,
			String testAddress,
			@RequestParam("courseware") CommonsMultipartFile courseware,
			@RequestParam("video") CommonsMultipartFile video,
			@RequestParam("homework") CommonsMultipartFile homework,
			@RequestParam("courseMaterial") CommonsMultipartFile courseMaterial,
			@RequestParam("courseAnswer") CommonsMultipartFile courseAnswer
			) throws IOException {
		
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try { 
			FileUpload fileUpload = new FileUpload();
			//上传课件，视频，作业(20170622将以下方法改为map类型,并且将作业对应的word文档也修改)
			Map<String,Object> uploadMapOne = fileUpload.fileUploadParam(courseware, request);
			String coursewareName = (String) uploadMapOne.get("result");
			Map<String,Object> uploadMapTwo = fileUpload.fileUploadParam(video, request);
			String videoName = (String) uploadMapTwo.get("result");
			Map<String,Object> uploadMapThree = fileUpload.fileUploadParam(homework, request);
			String homeworkName = (String) uploadMapThree.get("result");
			String homeworkNameDoc = (String) uploadMapThree.get("resultDoc");
			//上传备课材料和上传课后答案20170815商莹改
			Map<String,Object> uploadMapForuth = fileUpload.fileUploadParam(courseMaterial, request);
			String courseMaterialName = (String) uploadMapForuth.get("result");
			Map<String,Object> uploadMapForth = fileUpload.fileUploadParam(courseAnswer, request);
			String courseAnswerName = (String) uploadMapForth.get("result");
			resultMap.put("courseId", courseId);
			resultMap.put("chapterName", chapterName);
			resultMap.put("type", type);
			resultMap.put("testAddress", testAddress);
			resultMap.put("coursewareName", coursewareName);
			resultMap.put("videoName", videoName);
			resultMap.put("homeworkName", homeworkName);
			resultMap.put("homeworkNameDoc", homeworkNameDoc);
			resultMap.put("chapterNum", chapterNum);
			resultMap.put("addTestMark", addTestMark);
			resultMap.put("courseMaterialName", courseMaterialName);
			resultMap.put("courseAnswerName", courseAnswerName);
			//添加章节
			boolean addResult = chapterActionImpl.addChapter(resultMap);
			//成功,跳转修改页面
			if(addResult){
				reJsonStr="redirect:/skipSaveCourse?jsonStr=" + "%7B%22courseId%22:" + courseId + "%7D";
			//失败，跳转错误页面	
			}else{
				reJsonStr="redirect:/skipErrorPage";
			}		
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加章节出现异常", e);
		} finally {
		}
		return reJsonStr;
	}
	
	/***
	 * @description 修改章节
	 * @param request
	 * @param courseId
	 * @param chapterId
	 * @param chapterNum第几章
	 * @param chapterName
	 * @param type
	 * @param testAddress
	 * @param courseware
	 * @param video
	 * @param homework
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/saveChapterMessage", method = RequestMethod.POST)
	public String  saveChapterMessage  (HttpServletRequest request,long courseId,long chapterId,long chapterNum,long addTestMark,String chapterName,long type,String testAddress,
			@RequestParam("courseware") CommonsMultipartFile courseware,
			@RequestParam("video") CommonsMultipartFile video,
			@RequestParam("homework") CommonsMultipartFile homework,
			@RequestParam("courseMaterial") CommonsMultipartFile courseMaterial,
			@RequestParam("courseAnswer") CommonsMultipartFile courseAnswer,
			String delHomework,String delVideo,
			String delMaterial,String delAnswer
			) throws IOException {
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		FileUpload fileUpload = new FileUpload();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		boolean result = false;
		try { 
			//是否删除作业，视频的方法
			courseActionImpl.delHomeworkOrVideo(delHomework, delVideo, delMaterial,delAnswer,courseId , chapterId,chapterNum);
			//构建参数存数据库
			resultMap.put("courseId", courseId);
			resultMap.put("chapterId", chapterId);
			resultMap.put("chapterName", chapterName);
			resultMap.put("type", type);
			resultMap.put("testAddress", testAddress);
			resultMap.put("chapterNum", chapterNum);
			resultMap.put("addTestMark", addTestMark);
			//上传课件，视频，作业(20170622将以下方法改为map类型,并且将作业对应的word文档也修改)
			Map<String,Object> uploadMapOne = fileUpload.fileUploadParam(courseware, request);
			String coursewareName = (String) uploadMapOne.get("result");
			Map<String,Object> uploadMapTwo = fileUpload.fileUploadParam(video, request);
			String videoName = (String) uploadMapTwo.get("result");
			Map<String,Object> uploadMapThree = fileUpload.fileUploadParam(homework, request);
			String homeworkName = (String) uploadMapThree.get("result");
			String homeworkNameDoc = (String) uploadMapThree.get("resultDoc");
			//上传备课材料和上传课后答案20170815商莹改
			Map<String,Object> uploadMapForuth = fileUpload.fileUploadParam(courseMaterial, request);
			String courseMaterialName = (String) uploadMapForuth.get("result");
			Map<String,Object> uploadMapForth = fileUpload.fileUploadParam(courseAnswer, request);
			String courseAnswerName = (String) uploadMapForth.get("result");
			
			//修改，如果是新增的，不进行删除文件操作
			if(chapterId != 0){
				//删除之前上传的文件（如果没有修改，不删除）
			    List<Map<String, Object>> chapterList = chapterActionImpl.getCourseChapter(resultMap);
			    fileUpload.deleteFile(chapterList,courseware,video,homework,courseMaterial,courseAnswer);
				//处理文件名（没上传要用原来数据库的名字）
				String fileName = chapterActionImpl.getFileName(courseware, chapterId, "coursewareName", coursewareName,courseId);
				resultMap.put("coursewareName", fileName);
				fileName = chapterActionImpl.getFileName(video, chapterId, "videoName", videoName,courseId);
				resultMap.put("videoName", fileName);
				fileName = chapterActionImpl.getFileName(homework, chapterId, "homeworkName", homeworkName,courseId);
				resultMap.put("homeworkName", fileName);
			    fileName = chapterActionImpl.getFileName(homework, chapterId, "homeworkNameDoc", homeworkNameDoc,courseId);
				resultMap.put("homeworkNameDoc", fileName);
				fileName = chapterActionImpl.getFileName(courseMaterial, chapterId, "courseMaterialName", courseMaterialName,courseId);
				resultMap.put("courseMaterialName", fileName);
				fileName = chapterActionImpl.getFileName(courseAnswer, chapterId, "courseAnswerName", courseAnswerName,courseId);
				resultMap.put("courseAnswerName", fileName);
				//修改章节
				result = chapterActionImpl.saveChapter(resultMap);
			//添加章节
			}else{
				//保存课件，视频，作业
				resultMap.put("coursewareName", coursewareName);
				resultMap.put("videoName", videoName);
				resultMap.put("homeworkName", homeworkName);
				resultMap.put("homeworkNameDoc", homeworkNameDoc);
				resultMap.put("courseMaterialName", courseMaterialName);
				resultMap.put("courseAnswerName", courseAnswerName);
				//添加章节
				result = chapterActionImpl.addChapter(resultMap);
			}
			//成功，目前跳转到保存课程
			if(result){
				reJsonStr="redirect:/skipSaveCourse?jsonStr=" + "%7B%22courseId%22:" + courseId + "%7D";
			//失败，跳转到错误页面	
			}else{
				reJsonStr="redirect:/skipErrorPage";
			}		
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改章节出现异常", e);
		} finally {
		}
		return reJsonStr;
	}
	
	/**
	 * @description 删除章节
	 * @param request   courseId  chapterNum
	 * @param response  
	 * @return  成功 失败
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delChapter", method = RequestMethod.POST)
	public void delChapter(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //删除章节
			Map<String, Object> flag = chapterActionImpl.delChapter(paramMap);
			resultMark = ((Boolean)flag.get("result")) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", flag.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除章节出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	
	/**商莹
	 * @description 删除章节中的课件、作业、视频、备课材料、课后答案
	 * @param request   courseId  chapterNum type 1课件  2视频  3作业 5备课材料 6课后题， chapterId,
	 * @param response  
	 * @return  成功 失败
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delChapterCoHoVo", method = RequestMethod.POST)
	public void delChapterCoHoVo(HttpServletRequest request,HttpServletResponse response,String jsonStr) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //删除章节
			Map<String, Object> flag = chapterActionImpl.delChapterCoHoVo(paramMap);
			resultMark = ((Boolean)flag.get("result")) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", flag.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	
	/**
	 * @description 将课程变为不是选课的 或是选课的（没用上）
	 * @param request   courseId  chapterNum
	 * @param response  
	 * @return  成功 失败
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveCourseSelect", method = RequestMethod.POST)
	public void saveCourseSelect(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {		
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //保存选课标识
			boolean flag = courseActionImpl.saveSelectMark(paramMap);
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("保存选课标识出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 跳转到错误页面
	 * @return  
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipErrorPage", method = RequestMethod.GET)
	public ModelAndView skipErrorPage (HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {	
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加课程页面名称
			pageName = "pc/pageerror";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转错误页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 教师 课程管理
	 * @param request
	 * @param response
	 * @return  跳转到课程管理
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipTeaCourseMana", method = RequestMethod.GET)
	public ModelAndView skipTeaCourseMana(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取登录人角色信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"roleId","stuorteaId"}, request);
			resultMap = sessionValues;
			resultMap.put("inMark", 1);
			//获取我的课程信息
			List<Map<String, Object>> courseList = myCourseActionImpl.getMyCourse(resultMap);
			resultMap.put("courseList", courseList);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//页面名称
			pageName = "pc/courseMana";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转课程管理页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 进入结课管理列表
	 * @param request
	 * @param response
	 * @return  
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipTeaEndClassList", method = RequestMethod.GET)
	public ModelAndView skipTeaEndClassList(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取登录人角色信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			//获取教师的所有班级（从中间表里查）
			List<Map<String, Object>> classList = homeworkTestActionImpl.getTeaTeachClass(sessionValues);
			resultMap.put("classList", classList);
			paramMap.put("stuorteaId", sessionValues.get("stuorteaId"));
			//获取该教师教的班级的课程信息
			Map<String, Object> classCourseMap = courseActionImpl.getMyClassCourseList(paramMap);
			if(classCourseMap != null){
				resultMap.put("classCourseList", classCourseMap.get("classCourseList"));
				resultMap.put("totalPage", classCourseMap.get("totalPage"));
				resultMap.put("pageNow", classCourseMap.get("pageNow"));
			}
			
			//返回条件
			resultMap.put("pageSize", paramMap.get("pageSize"));
			resultMap.put("classId", paramMap.get("classId"));
			resultMap.put("classEndMark", paramMap.get("classEndMark"));
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//页面名称
			pageName = "pc/endClassList";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转结课管理页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 点击结课，进行结课操作，参数是班级id，课程id
	 * @param request 
	 * @param response
	 * @return 
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/endCourseClass", method = RequestMethod.POST)
	public void  endCourseClass (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值 
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//结课
			boolean saveResult = courseActionImpl.endCourseClass(paramMap,request);
			//返回参数
			resultMark = (saveResult) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("结课出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 获取某个老师教的全部课程
	 * @param request   需要老师的id
	 * @param response
	 * @return 
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getMyClasses", method = RequestMethod.POST)
	public void  getMyClasses (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(CourseAction.class.getName());
		try { 
			//获取参数值 
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			List<Map<String, Object>> courseList = courseActionImpl.getMyClasses(paramMap);
			resultMap.put("courseList", courseList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取教师教的课程出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	@RequestMapping(value = "/skill/model-import", method = RequestMethod.POST)
	@ResponseBody
	public Result importSkillExcel(
			@RequestParam(value = "file")  MultipartFile file,
			@RequestParam(value = "skillId", required = false) Integer skillId,
			@RequestParam(value = "role", required = false) Integer role) throws Exception {

		// 解析excel文件
		List<SkillDTO> allByExcel = ExcelsUtil.getAllByExcel(
				file, 0, 1);
		if (CollectionUtils.isEmpty(allByExcel)) {
			return Result.error(ResultEnum.SERVER_ERROR, "excel暂无添加数据，请重新上传");
		}
		//入库
		return Result.success(courseActionImpl.insertSkill(allByExcel, skillId, file, role));
	}

	@RequestMapping(value = "/querySkillInfo", method = RequestMethod.GET)
	@ResponseBody
	public Result queryClassInfo(@RequestParam Integer skillId,HttpServletResponse response, HttpServletRequest request) {
		List<SkillNotes> list = courseActionImpl.querySkill(skillId);
		return Result.success(list);
	}

	@RequestMapping(value = "/updateSkillInfo", method = RequestMethod.GET)
	@ResponseBody
	public Result updateSkillInfo(@RequestParam Integer skillId,
								  @RequestParam String name) {
		courseActionImpl.updateSkill(skillId, name);
		return Result.success();
	}

	@RequestMapping(value = "/querySkillOther", method = RequestMethod.GET)
	@ResponseBody
	public Result querySkillOther(@RequestParam(value = "skillId", required = false) Integer skillId,HttpServletResponse response, HttpServletRequest request) {
		Map<String, List<SkillNotes>> list = courseActionImpl.querySkillOther(skillId);
		return Result.success(list);
	}
	
}